﻿// <copyright file="SensorTelegram.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author>

namespace EnOcean.Radio.Sensor
{
  using System;

  /// <summary>
  /// Base class for sensor telegrams.
  /// </summary>
  [Serializable]
  public abstract class SensorTelegram : RadioTelegram
  {
    /// <summary>
    /// Variable for the Id property.
    /// </summary>
    private long id;

    /// <summary>
    /// Gets or sets the 32 bit transmitter ID. The ID must be in the range 0 to 0xffffffff inclusive.
    /// </summary>
    public long Id
    {
      get
      {
        return this.id;
      }

      set
      {
        if (value >= 0x0 && value <= 0xffffffff)
        {
          this.id = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("Id", "must be a 32 bit value in the range 0x0 to 0xffffffff inclusive");
        }
      }
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      byte[] i;

      base.CreateMessage(message);

      i = BitConverter.GetBytes(this.Id);
      message[8] = i[3]; // Id 3
      message[9] = i[2]; // Id 2
      message[10] = i[1]; // Id 1
      message[11] = i[0]; // Id 0
    }
  }
}
